<?php
namespace App\Exports;

use App\Models\Goods;
use App\Models\OrderToGoods;
use App\Models\PurchaseToMaterial;
use App\Models\RecordMaterial;
use Carbon\Carbon;
use Encore\Admin\Facades\Admin;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Maatwebsite\Excel\Concerns\FromView;

/**
 * Created By FreeAbrams
 * Date: 2020/11/26
 */
class MaterialExport implements FromView
{
	
	protected $timeBetween = [];
	
	public function __construct($timeBetween)
	{
		$this->timeBetween = $timeBetween;
	}
	
	public function view():View
	{
		$temp = $this->data($this->timeBetween);
		return view('admin.export.material-export-table', [
			'input' => $temp['input'],
			'makeGoods' => $temp['makeGoods'],
			'output' => $temp['output']
			]);
	}
	
	public function data($timeBetween)
	{
		$store_id = Admin::user()->store_id;
		$timeStart = new Carbon();
	    $timeEnd = new Carbon();
	    $timeBetween = [$timeStart->startOfYear(), $timeEnd->endOfYear()];
		//$timeBetween = [];
		// 进仓库原料价格
		$input = PurchaseToMaterial::whereBetween('created_at', $timeBetween)
				->with(['recordmaterial' => function($q){
					$q->where('type', 1);// 1采购2消耗
				}])
			    ->selectRaw('Month(created_at) as date, sum(unit_price * number) as price')
			    ->groupBy('date')
			    ->get();
		//dd($input);
		// 生成商品原料价格
		$makeGoods = PurchaseToMaterial::whereBetween('created_at', $timeBetween)
			    ->whereHas('recordmaterial', function (Builder $query){
			        $query->where('type', 2);// 1采购2消耗
			    })
			    ->selectRaw('Month(created_at) as date, sum(unit_price * number) as price')
			    ->groupBy('date')
			    ->get();
		$goods = OrderToGoods::with(['goods'])->whereBetween('created_at', $timeBetween)
			->get();
//		$goods = OrderToGoods::with(['goods' => function($q) use ($store_id){
//			$q->withTrashed()->where('store_id', $store_id)->with(['goodsToMaterials' => function($q) {
//				$q->with(['purchaseToMaterials' => function($q){
//					$q->orderBy('created_at', 'desc');
//				}])->withPivot('number')
//				;}]);
//		}])->whereBetween('created_at', $timeBetween)
//			->get();
//		dd($goods);
		$goods = $goods->map(function ($item, $key) {
			$time = New Carbon($item->created_at);
			$item->date = (int)$time->format('m');
			return $item;
		});
		foreach ($goods->groupBy('date') as $k => $v){
			static  $output = [];
			$output[$k] = $this->outputPrice($v->groupBy('goods_id'));
		}
		
		//$output = collect($goodsPrice)->sum();
		//dd($input, $makeGoods, $output);
		return $data = [
			'input' => $input,
			'makeGoods' => $makeGoods,
			'output' => $output
		];
	}
	
	// 计算毛利润
	public function outputPrice($groupByGoodsId)
	{
		//$goods->groupBy('goods_id')
		foreach ($groupByGoodsId as $k => $v)
		{
			// 卖出的商品数量
			static $goodsCollection = [];
			// 毛利润
			static $goodsPrice = [];
			if (empty($v[0]->goods)) {
				return 0;
			}
			$goodsCollection[$v[0]->goods->name] = $v->count('number');
			$goodsPrice[$v[0]->goods->name] = 0;
			$goodsNum = $goodsCollection[$v[0]->goods->name];
			// 商品材料用量
			static $goodsMaterialCost = [];
			foreach ($v as $key => $val){
				$goodsToMaterials = $val->goods->goodsToMaterials;
				$number = 0;
				$unit_price = 0;
				if (empty($goodsToMaterials->toArray())) {
					return $goodsPrice[$v[0]->goods->name] = 0;
				}
				foreach ($goodsToMaterials as $value)
				{
					// 判断用料是否已经超过第一批库存生成商品数 = 第一批库存毛利 + 第二批毛利 + 第三..
					foreach ($value->purchaseToMaterials as $purchase) {
						$price = 0;
						if($goodsNum - $purchase->sell > 0){
							$price += $purchase->sell * $purchase->unit_price;
							$goodsNum = $goodsNum - $purchase->sell;
						} else {
							$price += $goodsNum * $purchase->unit_price;
							$goodsPrice[$v[0]->goods->name] = $price;
							break;
						}
					}
				}
			}
		}
		return collect($goodsPrice)->sum();
	}
}